﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Site.Models;
using System.Web.Security;

namespace Site.Controllers
{

    // [Authorize(Roles="Administrator")]
    public class AdminClanController : Controller
    {
        private BMSiteDBEntities _db = new BMSiteDBEntities();
        //
        // GET: /AdminClan/

        public ActionResult Index()
        {

            var ListIds = (from c in _db.CLAN

                           select c.CLAN_ID);


            List<ClanAdmin> model = new List<ClanAdmin>();
            foreach (Guid m in ListIds)
            {
                model.Add(new ClanAdmin
                {
                    
                    clans = _db.CLAN.First(p => p.CLAN_ID == m),
                    /*users = _db.aspnet_Users.First(p => p.UserId == (from c in _db.CLAN where c.CLAN_ID == m select c).FirstOrDefault().CLAN_LEADER_ID),
                    */
                    stats = _db.STATISTIC.First(p => p.STAT_CLAN_ID == m)
                });


              
            }
            return View(model);
        }

        //
        // GET: /AdminClan/Details/5

        public ActionResult Details(Guid id)
        {
            var leaderId = (from c in _db.CLAN
                              where c.CLAN_ID==id
                                select c.CLAN_LEADER_ID).First();
            var ClanToshow = new ClanAdmin
            {

                users = _db.aspnet_Users.First(p => p.UserId == leaderId),
                clans = _db.CLAN.First(p => p.CLAN_ID == id),
                /*stats = _db.STATISTIC.First(p => p.STAT_USER_ID == id)*/

            };

            return View(ClanToshow);
        }

        //
        // GET: /AdminClan/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /AdminClan/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /AdminClan/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            var ClanToEdit = (from c in _db.CLAN
                                where c.CLAN_ID == id
                                select c).First();
            return View(ClanToEdit);
        }

        //
        // POST: /AdminClan/Edit/5

        [HttpPost]
        public ActionResult Edit(Guid id, CLAN ClanToEdit)
        {
            try
            {
                var OriginalClan = (from c in _db.CLAN
                                   where c.CLAN_ID == id
                                   select c).First();
                ClanToEdit = OriginalClan;

                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AdminClan/Delete/5
        //[Authorize(Roles = "ClanLeader")]
        public ActionResult Delete(Guid id)
        {
            var ClanToDelete = (from m in _db.CLAN
                              where m.CLAN_ID == id
                              select m).First();
            return View(ClanToDelete);
            
        }

        //
        // POST: /AdminClan/Delete/5
        //[Authorize(Roles = "ClanLeader")]
        [HttpPost]
        public ActionResult Delete(Guid id, CLAN ClanToDelete)
        {
            try
            {
                // TODO: Add delete logic here
                 ClanToDelete = (from m in _db.CLAN
                                  where m.CLAN_ID == id
                                  select m).First();
                 var ClanDeleted = ClanToDelete;
                 ClanDeleted.CLAN_DELETEDATE = DateTime.Now;
                 _db.ApplyCurrentValues(ClanToDelete.EntityKey.EntitySetName, ClanDeleted);
                 _db.SaveChanges();
                
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Unlock(Guid id, CLAN ClanToUnlock)
        {
            try
            {
                // TODO: Add delete logic here
                ClanToUnlock = (from m in _db.CLAN
                                where m.CLAN_ID == id
                                select m).First();
                var ClanUnlocked = ClanToUnlock;
                ClanUnlocked.CLAN_DELETEDATE =null;
                _db.ApplyCurrentValues(ClanToUnlock.EntityKey.EntitySetName, ClanUnlocked);
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // [Authorize(Roles="ClanLeader, ClanOfficer")]

        public ActionResult SingleClanDetails(Guid id)
        {
           var singleClan = (from m in _db.CLAN
                            where m.CLAN_ID == id
                            select m).First();
            return PartialView(singleClan);
        }
        
        public ActionResult InviteUser(Guid user)
        {

            return View();
        }

         public ActionResult KickUser(Guid userId)
         {
             return View();
         }
         public ActionResult KickUser(Guid userId, aspnet_Users OriginalUser)
         {
              OriginalUser = (from u in _db.aspnet_Users
                     where u.UserId == userId
                     select u).First();
             var UserToKick = OriginalUser;
             UserToKick.USER_CLAN_ID = null;
             _db.ApplyCurrentValues(OriginalUser.EntityKey.EntitySetName, UserToKick);
             _db.SaveChanges();
             Roles.RemoveUserFromRole(UserToKick.UserName, "ClanOfficer");
             
             return View();
         }

         public ActionResult ListMembers()
         {
             var CurrentUserClanId = (from l in _db.aspnet_Users
                                     where l.UserName== HttpContext.User.Identity.Name
                                     select l.USER_CLAN_ID).First();
             var MembersList = (from l in _db.aspnet_Users
                                where l.USER_CLAN_ID == CurrentUserClanId
                                select l).ToList();
             return View(MembersList);
         }

    }
}
